﻿using CefSharp;
using KeZhou.Models.Instances;
using KeZhou.Models.Wishire;
using KeZhou.ViewModels;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace KeZhou.Views.Components.Controls
{
    /// <summary>
    /// MainOneGuide.xaml 的交互逻辑
    /// </summary>
    public partial class MainOneGuide : Page
    {
        private static MainOneGuide instance;
        public static MainOneGuide Instance
        {
            get
            {
                return instance ?? (instance = new MainOneGuide());
            }
        }
        public MainOneGuide()
        {
            InitializeComponent();
            instance = this;
            Browser.Address = "http://101.35.231.245:1003/#/FirstPage";
           /* Browser.Address = "http://localhost:1003/#/FirstPage";*/
            Browser.JavascriptObjectRepository.Settings.LegacyBindingEnabled = true;
            Browser.JavascriptObjectRepository.NameConverter = null;
            Browser.JavascriptObjectRepository.Register("bound", new ExampleInstance(), options: BindingOptions.DefaultBinder);

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            List<Example> exampleList = new List<Example>();
            exampleList.Add(new Example("example1",1));
            exampleList.Add(new Example("example2", 2));

            // 将对象数组转换为JSON字符串
            var json = JsonConvert.SerializeObject(exampleList);

            Browser.ExecuteScriptAsyncWhenPageLoaded($"document.wpfCallVue({json});");
        }

    }

    class ExampleInstance
    {
        //vue调取wpf
        public void vueCallWpf(Example example)
        {
            /*Application.Current.Dispatcher.Invoke(() =>
            {
                MainOneGuide.Instance.tb.Text = example.v1;
            });*/
        }
    }

    class Example
    {
        public string v1;
        public int v2;

        // 无参数构造函数
        public Example()
        {
            // 设置默认值或执行其他初始化操作
            v1 = "";
            v2 = 0;
        }

        public Example(string v1, int v2)
        {
            this.v1 = v1;
            this.v2 = v2;
        }
    }
}
